<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.title" placeholder="Title" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        查询
      </el-button>
      <el-button :loading="downloadLoading" class="filter-item" type="primary" icon="el-icon-download" @click="handleDownload">
        导出
      </el-button>
    </div>

    <el-table v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 100%">
      <el-table-column align="center" label="ID" width="80">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="商品id" width="80">
        <template slot-scope="scope">
          <span>{{ scope.row.product.id }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="地址id" width="80">
        <template slot-scope="scope">
          <span>{{ scope.row.address.id }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="用户id" width="80">
        <template slot-scope="scope">
          <span>{{ scope.row.user.id }}</span>
        </template>
      </el-table-column>

      <el-table-column width="120px" align="center" label="是否付款">
        <template slot-scope="scope">
          <span>{{ scope.row.pay }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="商品件数">
        <template slot-scope="scope">
          <span>{{ scope.row.productNum }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="物流状态">
        <template slot-scope="scope">
          <span>{{ scope.row.status }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="评论">
        <template slot-scope="scope">
          <span>{{ scope.row.comment }}</span>
        </template>
      </el-table-column>

      <el-form-item label="评分" prop="comment">
        <el-input v-model="temp.rating" />
      </el-form-item>
      <el-table-column align="center" label="提交日期">
        <template slot-scope="scope">
          <span>{{ scope.row.createDate }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="{row,$index}">
          <el-button type="primary" size="medium " :disabled="row.status!=='已支付'" @click="handleShipOrder(row.id,$index)">
            发货
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.size"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { fetchMerchantOrder, shipOrder } from '@/api/order'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination

export default {
  name: 'OrderList',
  components: { Pagination },
  filters: {
    statusFilter(status) {
      const statusMap = {
        0: 'danger',
        1: 'success'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      downloadLoading: false,
      listQuery: {
        page: 1,
        size: 20
      },
      dialogStatus: 'update',
      dialogFormVisible: false,
      textMap: {
        update: 'Edit',
        create: 'Create'
      },
      temp: {
        name: 'temp',
        pay: false,
        status: 'temp',
        comment: 0
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      const { page, size } = this.listQuery
      fetchMerchantOrder({ page: page - 1, size }).then(response => {
        this.list = response.items
        this.total = response.total
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleShipOrder(orderId, idx) {
      shipOrder({ orderId }).then(res => {
        if (res.success) {
          this.list[idx].status = '已发货'
        } else {
          this.$notify({
            title: '失败',
            message: '发货失败',
            type: 'warning',
            duration: 2000
          })
        }
      })
    },
    handleDownload() {
      this.downloadLoading = true
      import('@/vendor/Export2Excel').then(excel => {
        const tHeader = ['id', 'pay', 'status', 'comment', 'createDate']
        const data = this.formatJson(tHeader)
        excel.export_json_to_excel({
          header: tHeader,
          data,
          filename: 'order-list'
        })
        this.downloadLoading = false
      })
    },
    formatJson(filterVal) {
      return this.list.map(v => filterVal.map(j => {
        return v[j]
      }))
    }
  }
}
</script>

<style scoped>
.edit-input {
  padding-right: 100px;
}
.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
</style>
